package com.gitee.thread.deadlock.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author ming.yan-1
 * @version V1.0
 * @Title com.gitee.thread.deadlock.readwrite
 * @Description
 * @date 2018/9/4  上午10:08
 */
public class ThreadDemo implements Runnable{
    private boolean isDead;
    private boolean isWrite;
    private ReentrantReadWriteLock reentrantReadWriteLock;
    private ReentrantReadWriteLock.WriteLock writeLock;
    private ReentrantReadWriteLock.ReadLock readLock;

    public ThreadDemo(boolean isDead, boolean isWrite, ReentrantReadWriteLock reentrantReadWriteLock) {
        this.isDead = isDead;
        this.isWrite = isWrite;
        this.reentrantReadWriteLock = reentrantReadWriteLock;
        this.readLock = reentrantReadWriteLock.readLock();
        this.writeLock = reentrantReadWriteLock.writeLock();
    }

    @Override
    public void run() {
        while (true){
//            if(isWrite){
//                writeLock.lock();
//                try{
//                    System.out.println("你说嘴巴嘟嘟");
//                }catch (Exception e){
//
//                }finally {
//                    writeLock.unlock();
//                }
//            }else {
//                readLock.lock();
//                try{
//                    System.out.println("嘟嘟嘟嘟嘟");
//                }catch (Exception e){
//
//                }finally {
//                    readLock.unlock();
//                }
//            }
            System.out.println("1");
                readLock.lock();
            System.out.println("2");
                writeLock.lock();
            System.out.println("3");
                readLock.unlock();
            System.out.println("4");
                writeLock.unlock();
            System.out.println("5");
        }

    }
}
